package com.drinkdelivery.front.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.drinkdelivery.common.config.RuoYiConfig;
import com.drinkdelivery.common.request.CommonPage;
import com.drinkdelivery.common.request.PageParamRequest;
import com.drinkdelivery.common.utils.file.FileUploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.drinkdelivery.common.annotation.Log;
import com.drinkdelivery.common.core.controller.BaseController;
import com.drinkdelivery.common.core.domain.AjaxResult;
import com.drinkdelivery.common.enums.BusinessType;
import com.drinkdelivery.common.domain.SysFileInfo;
import com.drinkdelivery.system.service.ISysFileInfoService;
import com.drinkdelivery.common.utils.poi.ExcelUtil;
import com.drinkdelivery.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件信息Controller
 * 
 * @author lzh
 * @date 2024-07-23
 */
@Api(tags = "文件信息")
@RestController
@RequestMapping("/front/info")
public class SysFileInfoController extends BaseController
{
    @Autowired
    private ISysFileInfoService sysFileInfoService;

    /**
     * 查询文件信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:info:list')")
    @ApiOperation(value = "查询文件信息列表")
    @GetMapping("/list")
    public CommonPage<SysFileInfo> list(SysFileInfo sysFileInfo, PageParamRequest pageParamRequest)
    {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        List<SysFileInfo> list = sysFileInfoService.selectSysFileInfoList(sysFileInfo);
        PageInfo<SysFileInfo> pageInfo = new PageInfo<>(list);
        return CommonPage.restPage(pageInfo);
    }

    /**
     * 导出文件信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:info:export')")
    @ApiOperation(value = "导出文件信息列表")
    @Log(title = "文件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysFileInfo sysFileInfo)
    {
        List<SysFileInfo> list = sysFileInfoService.selectSysFileInfoList(sysFileInfo);
        ExcelUtil<SysFileInfo> util = new ExcelUtil<SysFileInfo>(SysFileInfo.class);
        util.exportExcel(response, list, "文件信息数据");
    }

    /**
     * 获取文件信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:info:query')")
    @ApiOperation(value = "获取文件信息详细信息")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") Long fileId)
    {
        return success(sysFileInfoService.selectSysFileInfoByFileId(fileId));
    }

    /**
     * 新增文件信息
     */
    //@PreAuthorize("@ss.hasPermi('system:info:add')")
    @ApiOperation(value = "新增文件信息")
    @Log(title = "文件信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysFileInfo sysFileInfo)
    {
        return toAjax(sysFileInfoService.insertSysFileInfo(sysFileInfo));
    }

    /**
     * 修改文件信息
     */
    //@PreAuthorize("@ss.hasPermi('system:info:edit')")
    @ApiOperation(value = "修改文件信息")
    @Log(title = "文件信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysFileInfo sysFileInfo)
    {
        return toAjax(sysFileInfoService.updateSysFileInfo(sysFileInfo));
    }

    /**
     * 删除文件信息
     */
    //@PreAuthorize("@ss.hasPermi('system:info:remove')")
    @ApiOperation(value = "删除文件信息")
    @Log(title = "文件信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds)
    {
        return toAjax(sysFileInfoService.deleteSysFileInfoByFileIds(fileIds));
    }

    /**
     * 上传文件
     *
     * @param file
     * @param fileInfo
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "上传文件")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, SysFileInfo fileInfo) throws IOException
    {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        fileInfo.setFilePath(fileName);
        return toAjax(sysFileInfoService.insertSysFileInfo(fileInfo));
    }
}
